/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor. marco fresquisimo
 * 
 */

/*
 * BuscarCliente.java
 *
 * Created on Feb 5, 2012, 11:02:16 AM
 */

package INTERFAZ;

import CONTROLES.ControlCliente;
import CONTROLES.ControlSucursal;
import INTERFAZ.Bienvenida;
import MODELOS.Cliente;
import MODELOS.Sucursal;
import UTILITARIOS.Herramienta;
import UTILITARIOS.UTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;

/**
 *
 * @author VICENTE
 */
public class BuscarCliente extends javax.swing.JDialog {

    /** Creates new form BuscarCliente */
    public BuscarCliente(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        iniciar();
        addEventos();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        choice1 = new java.awt.Choice();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        uTextField1 = new UTextField(UTextField.INT_VALUE);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Busqueda de un CLIENTE ");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterios de busqueda"));
        jPanel3.setName("jPanel3"); // NOI18N

        choice1.setName("choice1"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jLabel2.setText("Elegir Sucursal");
        jLabel2.setName("jLabel2"); // NOI18N

        jButton1.setText("Buscar");
        jButton1.setName("jButton1"); // NOI18N

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jLabel6.setText("C.I. del Cliente:");
        jLabel6.setName("jLabel6"); // NOI18N

        uTextField1.setName("uTextField1"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(54, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(uTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(uTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                BuscarCliente dialog = new BuscarCliente(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Choice choice1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel3;
    private UTILITARIOS.UTextField uTextField1;
    // End of variables declaration//GEN-END:variables

    private Sucursal sucursal;
    private void iniciar() {

        sucursal = Bienvenida.sucursal;
        String sucursales[] = new ControlSucursal().nombresSuc();
        for(int i = 0; i<sucursales.length; i++)
           choice1.add(sucursales[i]);

        choice1.select(sucursal.getIdSucursal()+"-"+sucursal.getNombreSuc());

        if(Bienvenida.user.getTipoUsuario().equalsIgnoreCase("ADMINISTRADOR") && Bienvenida.user.getToken().equalsIgnoreCase("INGRESAR") ){
           choice1.setEnabled(true);
           }else{
           choice1.setEnabled(false);
           }
    }

    private void addEventos() {
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                StringTokenizer s = new StringTokenizer(choice1.getSelectedItem(),"-");
                int idSucursal = Integer.parseInt(s.nextToken());

                int numeroFactura = Integer.valueOf(uTextField1.getText());
                if(numeroFactura <= 0){
                    JOptionPane.showMessageDialog(null, "No Ha ingresado el CI del CLIENTE", "Confirmación", JOptionPane.ERROR_MESSAGE);
                }else{
                    Cliente f = new ControlCliente().dameClienteCiSuc(numeroFactura,idSucursal);
                    
                    if(f.getNombreCliente() == null){
                        
                        JOptionPane.showMessageDialog(null, "No se encontro al CLIENTE con C.I:"+numeroFactura, "Confirmación", JOptionPane.ERROR_MESSAGE);
                    } else {
                        dispose();
                        InformacionCliente v = new InformacionCliente(f);
                        Herramienta.centerOnScreen(v);
                        v.setVisible(true);
                    }
                }
            }
        });
    }

}
